﻿Public Class CategoriaForm

    Private dal As CeremoniaDAO = Nothing

#Region "Windows Form Events"

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        dal = New CeremoniaDAO()
    End Sub

    Private Sub UIBaseAdmin_Load(sender As System.Object, e As System.ComponentModel.CancelEventArgs) Handles MyBase.FormClosing
        If GuardarButton.Enabled Then
            If MessageBox.Show("Todos los cambios no guardados se perderan, ¿esta seguro de que desea cerrar el formulario?", "Cerrar Formulario", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub CancelarButton_Click(sender As System.Object, e As System.EventArgs) Handles CancelarButton.Click
        ManejaEvento(Eventos.CANCELAR)
    End Sub

    Private Sub BuscarButton_Click(sender As System.Object, e As System.EventArgs) Handles BuscarButton.Click, BuscarToolStripButton.Click
        ManejaEvento(Eventos.BUSQUEDA)
    End Sub


    Private Sub NuevoButton_Click(sender As System.Object, e As System.EventArgs) Handles NuevoButton.Click, NuevoToolStripButton.Click
        ManejaEvento(Eventos.CREACION)
    End Sub

    Private Sub ModificarButton_Click(sender As System.Object, e As System.EventArgs) Handles ModificarButton.Click, ModificarToolStripButton.Click
        ManejaEvento(Eventos.MODIFICACION)
        CargarFormularioDesdeDGV()
    End Sub

    Private Sub GuardarButton_Click(sender As System.Object, e As System.EventArgs) Handles GuardarButton.Click
        If validarInputGuardar() Then
            If modificacion Then
                ProcesarPeticion(Eventos.MODIFICACION)
            Else
                ProcesarPeticion(Eventos.CREACION)
            End If
            ManejaEvento(Eventos.CANCELAR)
        Else
            formularioConErrores()
        End If
    End Sub

    Private Sub CeremoniaForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        cargarDataGridView()
    End Sub

    Private Sub RecargarButton_Click(sender As System.Object, e As System.EventArgs) Handles RecargarButton.Click
        cargarDataGridView()
    End Sub

    Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        If Me.DataGridView1.SelectedRows.Count > 0 Then
            Me.EliminarButton.Enabled = True
            Me.EliminarToolStripButton.Enabled = True
            Me.ModificarButton.Enabled = True
            Me.ModificarToolStripButton.Enabled = True
        Else
            Me.EliminarButton.Enabled = False
            Me.EliminarToolStripButton.Enabled = False
            Me.ModificarButton.Enabled = False
            Me.ModificarToolStripButton.Enabled = False
        End If
    End Sub

    Private Sub EliminarButton_Click(sender As System.Object, e As System.EventArgs) Handles EliminarButton.Click, EliminarToolStripButton.Click
        Dim r As DialogResult = MessageBox.Show("¿Esta seguro que desea eliminar la ceremonia " & Me.IDCeremoniaTextBox.Text & "?", "Eliminar", MessageBoxButtons.YesNo)
        If r = Windows.Forms.DialogResult.Yes Then
            CargarFormularioDesdeDGV()
            ProcesarPeticion(Eventos.ELIMINACION)
            ManejaEvento(Eventos.ELIMINACION)
        End If
    End Sub

#End Region

#Region "Eventos de usuario"
    Public Overloads Sub ManejaEvento(ByVal evento As Eventos)
        Select Case evento
            Case Eventos.CANCELAR
                Me.modificacion = False
                Me.NuevoToolStripButton.Enabled = True
                Me.ModificarToolStripButton.Enabled = False
                Me.EliminarToolStripButton.Enabled = False
                Me.BuscarToolStripButton.Enabled = True
                Me.NuevoButton.Enabled = True
                Me.ModificarButton.Enabled = False
                Me.EliminarButton.Enabled = False
                Me.RecargarButton.Enabled = True
                Me.BuscarButton.Enabled = True
                Me.GuardarButton.Enabled = False
                Me.CancelarButton.Enabled = True
                Me.IDCeremoniaTextBox.Enabled = True
                Me.DateTimePicker1.Enabled = False
                Me.DateTimePicker2.Enabled = False
                Me.DescripcionTextBox.Enabled = False
                FormUtilerias.cleanTextBox(Me)
                Me.DateTimePicker1.Value = Today
                Me.DateTimePicker2.Value = Today
            Case Eventos.CREACION
                Me.modificacion = False
                Me.NuevoToolStripButton.Enabled = False
                Me.ModificarToolStripButton.Enabled = False
                Me.EliminarToolStripButton.Enabled = False
                Me.BuscarToolStripButton.Enabled = False
                Me.NuevoButton.Enabled = False
                Me.ModificarButton.Enabled = False
                Me.EliminarButton.Enabled = False
                Me.RecargarButton.Enabled = True
                Me.BuscarButton.Enabled = False
                Me.GuardarButton.Enabled = True
                Me.CancelarButton.Enabled = True
                Me.IDCeremoniaTextBox.Enabled = True
                Me.DateTimePicker1.Enabled = True
                Me.DateTimePicker2.Enabled = True
                Me.DescripcionTextBox.Enabled = True
            Case Eventos.ELIMINACION
                Me.modificacion = False
                ManejaEvento(Eventos.CANCELAR)
            Case Eventos.MODIFICACION
                Me.modificacion = True
                Me.NuevoToolStripButton.Enabled = False
                Me.ModificarToolStripButton.Enabled = True
                Me.EliminarToolStripButton.Enabled = False
                Me.BuscarToolStripButton.Enabled = False
                Me.NuevoButton.Enabled = False
                Me.ModificarButton.Enabled = True
                Me.EliminarButton.Enabled = False
                Me.RecargarButton.Enabled = True
                Me.BuscarButton.Enabled = False
                Me.GuardarButton.Enabled = True
                Me.CancelarButton.Enabled = True
                Me.IDCeremoniaTextBox.Enabled = False
                Me.DateTimePicker1.Enabled = True
                Me.DateTimePicker2.Enabled = True
                Me.DescripcionTextBox.Enabled = True
        End Select
    End Sub
#End Region


#Region "Seccion para validaciones de entrada de datos por parte del usuario"

    Public Function validarInputGuardar() As Boolean
        If Me.IDCeremoniaTextBox.Text <> vbNullString Or _
            Me.DateTimePicker1.Value > Me.DateTimePicker2.Value Or _
            Me.DescripcionTextBox.Text <> vbNullString Then
            Return True
        Else
            Return False
        End If
    End Function

#End Region

#Region "Proceso de datos"

    Public Sub cargarDataGridView()
        Me.DataGridView1.DataSource = dal.ObtenerCeremoniasGridView()
    End Sub

    Public Function formularioAObjeto() As Ceremonia
        Dim cer As Ceremonia = New Ceremonia()
        cer.id_ceremonia = Me.IDCeremoniaTextBox.Text
        cer.fecha_inicio_ceremonia = Me.DateTimePicker1.Value
        cer.fecha_fin_ceremonia = Me.DateTimePicker2.Value
        cer.descripcion_ceremonia = Me.DescripcionTextBox.Text
        Return cer
    End Function

    Private Sub ProcesarPeticion(ByVal evento As Eventos)
        Try
            Select Case evento
                Case Eventos.CREACION
                    dal.AgregarCeremonia(formularioAObjeto())
                    MessageBox.Show("Ceremonia guardada con exito")
                Case Eventos.ELIMINACION
                    dal.EliminarCeremonia(formularioAObjeto())
                    MessageBox.Show("Ceremonia eliminada con exito")
                Case Eventos.MODIFICACION
                    dal.ModificarCeremonia(formularioAObjeto())
                    MessageBox.Show("Ceremonia modificada con exito")
            End Select
            cargarDataGridView()
        Catch ex As Exception
            MessageBoxFactory.exceptionGeneral(ex)
        Finally
            ManejaEvento(Eventos.CANCELAR)
        End Try
    End Sub

    Private Sub CargarFormularioDesdeDGV()
        Me.IDCeremoniaTextBox.Text = Me.DataGridView1.SelectedRows(0).Cells("id_ceremonia").Value
        Me.DateTimePicker1.Value = CType(Me.DataGridView1.SelectedRows(0).Cells("fecha_inicio_ceremonia").Value, Date)
        Me.DateTimePicker2.Value = CType(Me.DataGridView1.SelectedRows(0).Cells("fecha_fin_ceremonia").Value, Date)
        Me.DescripcionTextBox.Text = Me.DataGridView1.SelectedRows(0).Cells("descripcion_ceremonia").Value
    End Sub


End Class
